import Koa from "koa";
import bodyParser from "koa-bodyparser";
import router from "./router/index.js";

const app = new Koa();

// 解析请求体
app.use(bodyParser());

// 注册路由
app.use(router.routes());
app.use(router.allowedMethods());

// 错误处理中间件
app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.status = err.statusCode || 500;
    ctx.body = { error: err.message || "Internal Server Error" };
    ctx.app.emit("error", err, ctx);
  }
});

// 启动服务器
app.listen(8080, () => {
  console.log("The server is running on http://127.0.0.1:8080");
});